package com.ruoyi.wms.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.UUID;

import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.wms.domain.WmsTransferDetail;
import com.ruoyi.wms.mapper.WmsTransferMapper;
import com.ruoyi.wms.domain.WmsTransfer;
import com.ruoyi.wms.service.IWmsTransferService;

/**
 * 调拨单Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-28
 */
@Service
public class WmsTransferServiceImpl implements IWmsTransferService 
{
    @Autowired
    private WmsTransferMapper wmsTransferMapper;

    /**
     * 查询调拨单
     * 
     * @param tranUuid 调拨单主键
     * @return 调拨单
     */
    @Override
    public WmsTransfer selectWmsTransferByTranUuid(String tranUuid)
    {
        return wmsTransferMapper.selectWmsTransferByTranUuid(tranUuid);
    }

    /**
     * 查询调拨单列表
     * 
     * @param wmsTransfer 调拨单
     * @return 调拨单
     */
    @Override
    public List<WmsTransfer> selectWmsTransferList(WmsTransfer wmsTransfer)
    {
        return wmsTransferMapper.selectWmsTransferList(wmsTransfer);
    }

    /**
     * 新增调拨单
     * 
     * @param wmsTransfer 调拨单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsTransfer(WmsTransfer wmsTransfer)
    {
        wmsTransfer.setCreateTime(DateUtils.getNowDate());
        wmsTransfer.setCreateBy(SecurityUtils.getUsername());
        int rows = wmsTransferMapper.insertWmsTransfer(wmsTransfer);
        insertWmsTransferDetail(wmsTransfer);
        return rows;
    }

    /**
     * 修改调拨单
     * 
     * @param wmsTransfer 调拨单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsTransfer(WmsTransfer wmsTransfer)
    {
        wmsTransfer.setUpdateTime(DateUtils.getNowDate());
        wmsTransferMapper.deleteWmsTransferDetailByTransUuid(wmsTransfer.getTranUuid());
        insertWmsTransferDetail(wmsTransfer);
        return wmsTransferMapper.updateWmsTransfer(wmsTransfer);
    }

    /**
     * 批量删除调拨单
     * 
     * @param tranUuids 需要删除的调拨单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsTransferByTranUuids(String[] tranUuids)
    {
        wmsTransferMapper.deleteWmsTransferDetailByTransUuids(tranUuids);
        return wmsTransferMapper.deleteWmsTransferByTranUuids(tranUuids);
    }

    /**
     * 删除调拨单信息
     * 
     * @param tranUuid 调拨单主键
     * @return 结果
     */
    @Override
    public int deleteWmsTransferByTranUuid(String tranUuid)
    {
        wmsTransferMapper.deleteWmsTransferDetailByTransUuid(tranUuid);
        return wmsTransferMapper.deleteWmsTransferByTranUuid(tranUuid);
    }

    /**
     * 新增调拨单明细信息
     * 
     * @param wmsTransfer 调拨单对象
     */
    public void insertWmsTransferDetail(WmsTransfer wmsTransfer)
    {
        List<WmsTransferDetail> wmsTransferDetailList = wmsTransfer.getWmsTransferDetailList();
        String tranUuid = wmsTransfer.getTranUuid();
        if (StringUtils.isNotNull(wmsTransferDetailList))
        {
            List<WmsTransferDetail> list = new ArrayList<WmsTransferDetail>();
            for (WmsTransferDetail wmsTransferDetail : wmsTransferDetailList)
            {
                wmsTransferDetail.setTransUuid(tranUuid);
                list.add(wmsTransferDetail);
            }
            if (list.size() > 0)
            {
                wmsTransferMapper.batchWmsTransferDetail(list);
            }
        }
    }
}
